package com.qianfeng.smartdevices.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.annotations.AopLogAnnotation;
import com.qianfeng.smartdevices.dto.R;
import com.qianfeng.smartdevices.pojo.Categories;
import com.qianfeng.smartdevices.service.CategoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/categories")
public class CategoriesController {

    private CategoriesService categoriesService;

    @Autowired
    public void setCategoriesService(CategoriesService categoriesService) {
        this.categoriesService = categoriesService;
    }

    @PostMapping("/category")
    @AopLogAnnotation("分类添加")
    public R addCategories(@RequestBody Categories categories) throws Exception {
        categoriesService.addCategories(categories);
        return R.setOK();
    }
    @GetMapping("/categories")
    @AopLogAnnotation("分类分页查询")
    public R findAllCategoriesByCategories(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,String categoryname,Long status){

        PageInfo<Categories> categoriesByPage = categoriesService.findCategoriesByPage(page, limit,categoryname,status);
        return R.setOK(categoriesByPage);
    }

    @DeleteMapping("/categories")
    @AopLogAnnotation("分类删除")
    public R deleteCategoriesById(Long[] ids) {

        categoriesService.deleteById(ids);
        return R.setOK();
    }

    @PutMapping("/category")
    @AopLogAnnotation("分类更新")
    public R updateCategories(@RequestBody Categories categories)  {
        categoriesService.updateCategories(categories);
        return R.setOK();
    }
}
